<!--
	Desc: 	人员信息表的编辑界面
	Author: caochaofeng
	Date: 	2018-08-19 17:21:27
-->

<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="所属公司" prop="ownedCompany">
        <el-input v-model="dataForm.ownedCompany" placeholder="所属公司"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>

      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
      </el-form-item>

      <el-form-item label="主要岗位" prop="jobNm">
        <el-input v-model="dataForm.jobNm" placeholder="主要岗位"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as API  from '@/api/pers'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          entpPk: '',
          ownedCompany: '',
          mobile: ''

        },
        dataRule: {
          name: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          entpPk: [
            {required: true, message: '企业主键不能为空', trigger: 'blur'}
          ],
          ownedCompany: [
            {required: true, message: '所属公司不能为空', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '手机号不能为空', trigger: 'blur'}
          ],
          jobNm: [
            {required: true, message: '主要岗位不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.info(this.dataForm.vecPk).then((data) => {
              if (data && data.code === 0) {
                this.dataForm = data.vec
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = this.dataForm
            var tick = !this.dataForm.id ? API.add(params) : API.update(params)
            tick.then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
